package com.battle.service.repository.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.dto.sirens.SelectSirensApiDto;
import com.battle.pojo.entity.SirensInfo;
import com.battle.pojo.vo.sirens.GetSirensInfoApiVo;
import com.battle.pojo.vo.sirens.GetSirensListApiVo;
import com.battle.pojo.vo.sirens.api.*;
import com.battle.service.config.HttpConfig;
import com.battle.service.mapper.SirensApiMapper;
import com.battle.service.repository.in.SirensApiRepository;
import com.google.gson.JsonArray;
import nirvana.core.repository.Repository;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 【描 述】：
 * 【环 境】：J2SE
 * 【详 细】：
 */
@Service
public class SirensApiRepositoryImpl extends Repository<SirensApiMapper, SirensInfo> implements SirensApiRepository {

    private final SirensApiMapper mapper;

    public SirensApiRepositoryImpl(SirensApiMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public Page<GetSirensListApiVo> selectSirensList(SelectSirensApiDto dto) {
        return mapper.selectSirensList(dto);
    }

    @Override
    public Page<GetSirensListApiVo> getSirensListByType(SelectSirensApiDto dto) {
        Page<GetSirensListApiVo> page=new Page<>();

        List<GetSirensListApiVo> voList=new ArrayList<>();
        Integer number=0;

        List<String> idsByType = mapper.getIdsByType(dto.getType());
        String sql ="";
        String numberSQL="";

        String address="";
        if (dto.getAddress()!=null){
            address=" and ddmc like '%"+dto.getAddress()+"%'";
        }

        if (idsByType.size() > 0) {
            sql=" select id,\n" +
                    "               lasj,\n" +
                    "               ddmc,\n" +
                    "               TO_CHAR(cdcls) as cdcls,\n" +
                    "               jqdjdm,\n" +
                    "               jqflydm,\n" +
                    "               jqzt,\n" +
                    "               ajbh,\n" +
                    "               xfjyjg_tywysbm\n" +
                    "        from IECS_HANGZHOU.JCJ_AJXX\n" +
                    "        where 1 = 1 " + address +
                    "        and id not in (" +"'"+String.join("','", idsByType) +"'"+ ") " +
                    "ORDER BY lasj DESC limit "+dto.getSize()*dto.getCurrent();

            numberSQL=" select count(*) as number from IECS_HANGZHOU.JCJ_AJXX\n" +
                    "        where 1 = 1  " + address +
                    "        and id not in (" +"'"+String.join("','", idsByType) +"'"+") ";
        }else{
            sql=" select id,\n" +
                    "               lasj,\n" +
                    "               ddmc,\n" +
                    "               TO_CHAR(cdcls) as cdcls,\n" +
                    "               jqdjdm,\n" +
                    "               jqflydm,\n" +
                    "               jqzt,\n" +
                    "               ajbh,\n" +
                    "               xfjyjg_tywysbm\n" +
                    "        from IECS_HANGZHOU.JCJ_AJXX\n" +
                    "        where 1 = 1 " + address +
                    " ORDER BY lasj DESC limit"+dto.getSize()*dto.getCurrent();

            numberSQL=" select count(*) as number from IECS_HANGZHOU.JCJ_AJXX\n" +
                    "        where 1 = 1  "+ address;
        }

        String url = HttpConfig.HttpURL;
        JSONObject body = new JSONObject();
        body.put("sql", sql);
        String result = HttpUtil.createPost(url)
                .body(body.toString())
                .execute().body();

        JSONObject resultJson = JSONObject.parseObject(result);
        if (resultJson != null) {
            JSONArray jsonArray = resultJson.getJSONArray("data");
            if (jsonArray!=null){
                if (jsonArray.size() > 0) {
                    for (int i = 0; i < jsonArray.size(); i++) {
                        GetSirensListApiVo vo = jsonArray.getObject(i, GetSirensListApiVo.class);
                        voList.add(vo);
                    }

                    body=new JSONObject();
                    body.put("sql",numberSQL);
                    result = HttpUtil.createPost(url)
                            .body(body.toString())
                            .execute().body();

                    resultJson = JSONObject.parseObject(result);
                    if (resultJson != null) {
                        JSONArray jsonArray1 = resultJson.getJSONArray("data");
                        if (jsonArray1.size() > 0) {
                            number = jsonArray1.getJSONObject(0).getInteger("number");
                        }
                    }
                }
            }

        }

        page.setRecords(voList);
        page.setTotal(number);
       return page;
//        return mapper.getSirensListByType(dto);
    }

    @Override
    public ParticularsCase selectParticularsCase(String id) {
        ParticularsCase particularsCase = new ParticularsCase();

        String url = HttpConfig.HttpURL;
        JSONObject body = new JSONObject();
        String sql = " SELECT a.id,a.bjjssj,a.bjdh,a.jjkssj,\n" +
                "               a.bjrlxdh,a.jqflydm,a.jjyxm,a.jjygh,\n" +
                "               a.bjfslbdm,a.ajbh,a.bjdz,\n" +
                "               a.lasj,a.ddmc,a.xzqhdm,a.jqdjdm,\n" +
                "               c.cjpc,TO_CHAR(c.cjdws) as cjdws,TO_CHAR(c.cdcls) as cdcls\n" +
                "        from IECS_HANGZHOU.JCJ_AJXX as a\n" +
                "                 left join IECS_HANGZHOU.JCJ_CJJL as c on a.id=c.jq_tywysbm\n" +
                "        where a.id= '" + id + "'";
        body.put("sql", sql);

        String result = HttpUtil.createPost(url)
                .body(body.toString())
                .execute().body();

        JSONObject resultJson = com.alibaba.fastjson.JSONObject.parseObject(result);
        if (resultJson != null) {
            JSONArray jsonArray = resultJson.getJSONArray("data");
            if (jsonArray.size() > 0) {
                particularsCase = jsonArray.getObject(0, ParticularsCase.class);
            }
        }
        return particularsCase;
//        return mapper.selectParticularsCase(id);
    }

    @Override
    public FieldFeedback selectFieldFeedback(String id) {
        FieldFeedback entity = new FieldFeedback();

        String url = HttpConfig.HttpURL;
        JSONObject body = new JSONObject();
        String sql = "select\n" +
                "            fkjg_dwmc,\n" +
                "            fkr_xm,\n" +
                "            jl_rqsj,\n" +
                "            jlr_xm,\n" +
                "            dc_rs,\n" +
                "            bk_rs,\n" +
                "            qzsw_rs,\n" +
                "            qzss_rs,\n" +
                "            dwss_rs,\n" +
                "            dwsw_rs,\n" +
                "            hc_wd,\n" +
                "            tqzklbdm,\n" +
                "            kq_wd,\n" +
                "            fxlbdm,\n" +
                "            fldjdm,\n" +
                "            xdsd,\n" +
                "            jzjg,\n" +
                "            rsc,\n" +
                "            zhdjdm,\n" +
                "            rs_mj,\n" +
                "            TO_CHAR(yaw_jyqk)as yaw_jyqk,\n" +
                "            TO_CHAR(xcmskz_jyqk) as xcmskz_jyqk\n" +
                "        from IECS_HANGZHOU.JCJ_XCXX where jq_tywysbm= '" + id + "'";
        body.put("sql", sql);

        String result = HttpUtil.createPost(url)
                .body(body.toString())
                .execute().body();

        JSONObject resultJson = com.alibaba.fastjson.JSONObject.parseObject(result);
        if (resultJson != null) {
            JSONArray jsonArray = resultJson.getJSONArray("data");
            if (jsonArray.size() > 0) {
                entity = jsonArray.getObject(0, FieldFeedback.class);
            }
        }
        return entity;

//        return mapper.selectFieldFeedback(id);
    }

    @Override
    public ClosingFeedback selectClosingFeedback(String id) {
        ClosingFeedback entity = new ClosingFeedback();

        String url = HttpConfig.HttpURL;
        JSONObject body = new JSONObject();
        String sql = "select\n" +
                "            afdz,\n" +
                "            zhr_xm,\n" +
                "            jzcsds,\n" +
                "            jzcsdx,\n" +
                "            jzmj,\n" +
                "            rsc,\n" +
                "            rswz_mc,\n" +
                "            ssrs,\n" +
                "            swrs,\n" +
                "            qjrs\n" +
                "        from IECS_HANGZHOU.JCJ_XCXX where jq_tywysbm= '"+id+"'";
        body.put("sql", sql);

        String result = HttpUtil.createPost(url)
                .body(body.toString())
                .execute().body();

        JSONObject resultJson = com.alibaba.fastjson.JSONObject.parseObject(result);
        if (resultJson != null) {
            JSONArray jsonArray = resultJson.getJSONArray("data");
            if (jsonArray.size() > 0) {
                entity = jsonArray.getObject(0, ClosingFeedback.class);
            }
        }
        return entity;
//        return mapper.selectClosingFeedback(id);
    }

    @Override
    public AlarmInformation selectAlarmInformation(String id) {
        AlarmInformation entity = new AlarmInformation();

        String url = HttpConfig.HttpURL;
        JSONObject body = new JSONObject();
        String sql = "select\n" +
                "            bjsj,\n" +
                "            bjfslbdm,\n" +
                "            jjyxm,\n" +
                "            jjygh,\n" +
                "            bjrlxdh,\n" +
                "            bjrxm,\n" +
                "            bjdz,\n" +
                "            jjkssj,\n" +
                "            ddmc,\n" +
                "            xzqhdm\n" +
                "        from IECS_HANGZHOU.JCJ_AJXX where id= '"+id+"'";
        body.put("sql", sql);

        String result = HttpUtil.createPost(url)
                .body(body.toString())
                .execute().body();

        JSONObject resultJson = com.alibaba.fastjson.JSONObject.parseObject(result);
        if (resultJson != null) {
            JSONArray jsonArray = resultJson.getJSONArray("data");
            if (jsonArray.size() > 0) {
                entity = jsonArray.getObject(0, AlarmInformation.class);
            }
        }
        return entity;
//        return mapper.selectAlarmInformation(id);
    }


}
